package tuiLayer;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Scanner;

import javax.swing.text.html.HTMLDocument.Iterator;

import modelLayer.*;
import controlLayer.*;

public class SaleUI{
    Scanner keyboard = new Scanner(System.in);
    SaleController saleController = new SaleController();
    ItemController itemController = new ItemController();
    CustomerController customerController = new CustomerController();
    String errorMessage = "Incorrect command!";
    private String message="";
    String username;
    Sale sale;
    int cprNo;
    HashMap<Integer, Integer> line = new HashMap<>();
    
   Customer customer = new RegularCustomer("Johnny", "acasa", "000", 12, false, 10);
    Item i = new NormalItem("10", 11, 12, 30, "31");
    Item i1 = new NormalItem("chair", 19.99, 13, 31, "hsdg");
    ItemContainer items = ItemContainer.getInstance();
    Staff s = new Staff("1", "2", 23, "3", "4", false);
    StaffContainer staffs = StaffContainer.getInstance();
    Item set = new Set("Kitchen", 10.99, 99);
    CustomerContainer customerContainer = CustomerContainer.getInstance();
    
    public SaleUI(){
        items.addNormalItem(i);
        items.addNormalItem(i1);
        staffs.addStaff(s);
        ((Set)set).addSetItem(itemController.findItem(12).getBarcode());
        ((Set)set).addSetItem(itemController.findItem(13).getBarcode());
        set.setQuantity(itemController.makeSetQuantity(set.getBarcode()));
        items.addSet(set);
        customerContainer.addCustomer(customer);
        writeSaleMenu();
        
    }
    public void writeSaleMenu(){
        System.out.println("***Start sale Menu***");
        System.out.println(" (1) Add employee");
        System.out.println(" (2) Return to main menu");
        
        String choice = getString();
        
        switch(choice){
            case "1":
               addEmployee();
               break;
            case "2":
                System.out.println(errorMessage);
                pressEnter();
               // writeMainMenu();
        }
    }
    private void addEmployee(){
        System.out.println("Enter username: ");
        username = getString();
        while(saleController.addStaff(username) == null){
            System.out.println("Enter valid username! ");
            username = getString();
        }
        pressEnter();
        addCustomer();
    }
    private void addCustomer(){
        System.out.println("Enter cprNo(0 if no customer): ");
        cprNo = Integer.parseInt(getString());
        while(saleController.addCustomer(cprNo) == null && cprNo != 0){
            System.out.println("Enter valid CPR No. (0 if no customer): ");
            cprNo = Integer.parseInt(getString());
        }
        pressEnter();
        addItem();
    }
    private void addItem(){
        boolean quit = false;
        System.out.println("***Start sale Menu***");
        System.out.println("Enter barcode or ID of product: ");
        int barcode = Integer.parseInt(getString());
        while(saleController.findItemByBarcode(barcode) == null){
            System.out.println("Enter valid barcode!");
            barcode = Integer.parseInt(getString());
        }
        line.put(barcode, 1);
        pressEnter();
        while(!quit){
            System.out.println("***Item Barcode Menu***");
            System.out.println(" (1) Enter barcode or ID of product:  ");
            System.out.println(" (2) Finish Sale ");
            String choice = getString();
            switch(choice){
                case "1":
                    System.out.println("Enter barcode or ID of product: ");
                    barcode = Integer.parseInt(getString());
                while(saleController.findItemByBarcode(barcode) == null){
                    System.out.println("Enter valid barcode!");
                    barcode = Integer.parseInt(getString());
                }
                if(line.get(barcode) != null)
                    line.put(barcode, line.get(barcode) +1);
                else line.put(barcode, 1);
                System.out.println("" + line.get(barcode));
                break;
                case "2":
                    makePayment();
                    quit=true;
                    break;
            }
            pressEnter();
        }
    }
    public void makePayment(){
        boolean quit = false;
        double amount;
        String announcer = "";
        sale = saleController.makeSale(username, cprNo, line);
        while(!quit){
        	System.out.println("Price: " + sale.getTotalPrice());
            System.out.println("Enter Money! ");
            amount = Double.parseDouble(getString());
            announcer = saleController.makePayment(sale, amount);
            if(!announcer.equals("Not Done Yet! More Money!")){
            	quit = true;
            System.out.println(announcer);
            printReceipt(sale, amount);
            }
        }
    }
    
    private void printReceipt(Sale sale,double amount){
    	boolean quit = false;
    	ArrayList<SaleLine> saleLines = sale.getSaleLines();
    	Date date = new Date();
    	System.out.println("###################");
    	System.out.println("***** Reciept *****");
    	System.out.println("Item x Quantity"+ "\tPrice");
    	for(int i=0; i < saleLines.size(); i++ ){
    		SaleLine sL = saleLines.get(i);
    		//System.out.print("" + (i+1) + " ");
    		System.out.print(saleController.findItemByBarcode(sL.getBarcode()).getName() + " x " + sL.getQuantity());
    		System.out.println("\t" + sL.getPrice());
    		if(itemController.findItem(saleLines.get(i).getBarcode()) instanceof Set){
    		for(Integer setItems: ((Set)itemController.findItem(saleLines.get(i).getBarcode())).getSetItems()){
    		System.out.println("\t\t"+itemController.findItem(setItems).getName());
    		}
    		}
    	}
    	java.util.Iterator<Integer> itr = line.keySet().iterator();
    	while(itr.hasNext()){ // while it still has keys to search for 
            int barcode = (int)itr.next();
          	}
    	System.out.print("\nAssisted By: "+ sale.getStaff().getName());
    	if(customerController.findCustomer(cprNo)!=null)
    	{
    		System.out.print("\nCustomer name: "+customerController.findCustomer(cprNo).getName());
    	//	System.out.print("\nIs craftsman: "+customerController.findCustomer(cprNo).getIsCraftsman());
    	}
    	System.out.println("\nDate of Purchase: "+ date.getDay() + "-" + date.getMonth() + "-" + (date.getYear()+1900) + '\n');
    	System.out.println("*** Money Paid: " + amount);
    	System.out.println("*** Price: " + sale.getTotalPrice());
    	System.out.println("*** Change due: " + (amount - sale.getTotalPrice()));
    	System.out.println("###################");
    }
    
    private void pressEnter(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Press enter to continue.");
        scanner.nextLine(); 
    }
    private String getString(){
        String startChoice ;
        startChoice = keyboard.next();
        String choice = startChoice.trim();
        return choice;
    }
}